<?php

class UrlFunctions
{
    public static function genUrl($parameters = array(), $absolute = false, $secure = null)
    {
        if ($absolute) {
            if ($secure === null)
                $secure = sfContext::getInstance()->getRequest()->isSecure();
            $prefix = $secure ? 'https://' : 'http://';
            return $prefix.sfConfig::get('sf_fss_base_domain').self::genRelativeUrl($parameters);
        } else {
            return self::genRelativeUrl($parameters);
        }
    }


    public static function genFbUrl($parameters = array(), $absolute = false, $app = null, $ssl = null)
    {
        return self::addCanvasPrefix(self::genRelativeUrl($parameters), $absolute, $app, $ssl);
    }


    public static function addCanvasPrefix($url, $absolute = false, $app = null, $ssl = null)
    {
        if ($app)
            $url = $app->canvas_name.$url;
        else
            $url = sfConfig::get('sf_facebook_app_url').$url;

        if ($absolute) {
            if ($ssl === null) {
                // SSL-ification isn't forced, use same protocal as current
                // request
                $ssl = sfContext::getInstance()->getRequest()->isSecure();
            }

            $beta = sfconfig::get('sf_facebook_enable_beta_tier') ? 'beta.' : '';
            $url = 'http'.($ssl ? 's' : '').'://apps.'.$beta.'facebook.com'.$url;
        } else if ($ssl !== null) {
            // Forcing the protocol only makes sense for absolute URLs
            vtnsLog::errorWithBT('addCanvasPrefix called with invalid parameters: $absolute=true, $ssl=false');
        }

        return $url;
    }


    private static function genRelativeUrl($parameters = array())
    {
        $url = sfContext::getInstance()->getController()->genUrl($parameters, false);

        $request = sfContext::getInstance()->getRequest();
        if ($request->getPathInfoPrefix()) {
            $bad_prefix = $request->getPathInfoPrefix();
            $bad_prefix = '#^'.preg_quote($bad_prefix, '#').'#';
            $url = preg_replace($bad_prefix, '', $url);
        }

        return $url;
    }


    /// addGetParameter will
    /// take the arguments (http://www.vnoel.com?cmd=list#here,'option','test')
    /// to make the url http://www.vnoel.com?cmd=list&option=test#here
    /// if the parameter is already present, by default it will override it
    public static function addGetParameter($url,$key,$value,$override=true)
    {
        // This only applies if parameter arrays are used
        if(!$override && strpos($url,"$key=")!==false)
        {
            return $url;
        }

        // Pick the correct separator to use
        $separator = "?";
        if (strpos($url,"?")!==false)
          $separator = "&";

        // Find the location for the new parameter
        $insertPosition = strlen($url);
        if (strpos($url,"#")!==false)
          $insertPosition = strpos($url,"#");

        // Build the new url
        $newUrl = substr_replace($url,"$separator$key=$value",$insertPosition,0);

        return $newUrl;
    }
}

